<?php
/**
 * @file name : my_string_helper.php
 * @package   :
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
    if (!defined('BASEPATH')) {
        exit('No direct script access');
    }
/**
 * clipString
 *
 */
if ( ! function_exists('strip_slashes'))
{
    function clipString($str, $len)
    {
        if (!is_string($str) || strlen($str) <= 0) {
            return '';
        }
        $str = strlen($str) < $len ? $str : substr($str, 0 , $len) . '...';

        return $str;
    }
}
    /**
     * @function    menuRecursive
     *
     * @param       array $arrCategory
     * @param       int $parent
     * @param       string $parentUrl
     * @param       string $childUrl
     * @param       string $ulCls
     * @param       string $liCls
     *
     * @return      string
     */
if ( ! function_exists('menuRecursive')) {
    function menuRecursive($arrCategory, $parent = 0, $parentUrl, $childUrl, $ulCls = '', $liCls = '')
    {
        $str = '';
        $last = '';
        if(isset($arrCategory[$parent]) && count($arrCategory[$parent]) > 0) {
            $str .= '<ul ' . $ulCls . '>' . "\n";
            foreach($arrCategory[$parent] as $category) {
                if ($last == $parent) {
                    $str .= '<li ' . $liCls . '><a href="' . $parentUrl . $category->category_id . '/'
                         . $category->category_name_url . '.html">' . $category->category_name . '</a>';
                    $str .= "\n";
                } else {
                    $str .= '<li ' . $liCls . '><a href="' . $childUrl . $category->category_id . '/'
                         . $category->category_name_url . '.html">' . $category->category_name . '</a>';
                }
                $str .= menuRecursive($arrCategory, $category->category_id, $parentUrl, $childUrl,
                                             $ulCls, $liCls);
                $str .= '</li>' . "\n";
            }
            $str .= '</ul>' . "\n";
        }
        return $str;
    }
}
    /**
     * @function    leftMenuRecursive
     *
     * @param       array $arrCategory
     * @param       int $parent
     * @param       string $parentUrl
     * @param       string $childUrl
     * @param       string $ulCls
     * @param       string $liCls
     *
     * @return      string
     */
if ( ! function_exists('leftMenuRecursive')) {
    function leftMenuRecursive($arrCategory, $parent = 0, $run = 0, $parentUrl, $childUrl, $ulCls = '', $liCls = '')
    {
        $str = '';
        $last = '';
        if(isset($arrCategory[$parent]) && count($arrCategory[$parent]) > 0) {

            foreach($arrCategory[$parent] as $category) {

                if ($last == $parent) {
                    if ($run == 0) {
                        $liCls = str_replace('class="menu-item"', 'class="first-menu-item"', $liCls);
                    }
                    $run = $run + 1;
                    if ($run == count($arrCategory[0])) {
                        $liCls = preg_replace('/class="menu-item"|class="first-menu-item"/', 'class="last-menu-item"', $liCls);
                    }
                    if ($run > 1) {
                        $liCls = preg_replace('/class="first-menu-item"/', 'class="menu-item"', $liCls);
                    }
                    $str .= '<li ' . $liCls . '><a href="' . $parentUrl . $category->category_id . '/'
                         .  $category->category_name_url .'.html">' . $category->category_name . '</a>';
                    $str .= "\n";
                } else {
                    $liCls = preg_replace('/class="first-menu-item"/', 'class="menu-item"', $liCls);
                    $str .= '<li ' . $liCls . '><a href="' . $childUrl . $category->category_id . '/'
                         .  $category->category_name_url . '.html">' . $category->category_name . '</a>';

                }
                if ($last == $parent && array_key_exists($category->category_id, $arrCategory)) {
                    $str .= '<ul ' . $ulCls . '>';
                }
                $str .= leftMenuRecursive($arrCategory, $category->category_id, $run, $parentUrl, $childUrl,
                                          $ulCls, $liCls);
                if ($last == $parent && array_key_exists($category->category_id, $arrCategory)) {
                    $str .= '</ul>' . "\n";
                }
                $str .= '</li>' . "\n";
            }
        }
        return $str;
    }
}